package com.iqiyi.pps.epg.core.model.stream;

import com.iqiyi.pps.epg.api.model.web.stream.TLiveStream;
import knowledge.pub.Properties;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

@Entity
@Table(name = "live_stream")
@Cache(usage = CacheConcurrencyStrategy.NONE)
public class LiveStream {
    public static final int STATUS_START = 1;
    public static final int STATUS_STOP = 2;
    public static final int STATUS_DELETED = -1;
    public static final String FORMATTYPE_FLV = "FLV";
    public static final String FORMATTYPE_HCDN = "HCDN";
    public static final String FORMATTYPE_TS = "TS";
    public static final String FORMATTYPE_MP4 = "MP4";
    public static final String FORMATTYPE_P2P = "P2P";
    public static final String FORMATTYPE_HLFLV = "HLFLV";
    public static final String FORMATTYPE_DOLBY = "DOLBY";

    private Long id;
    private Long fatherId;
    private String streamName;
    private Boolean isDefault;
    private String uuid;
    private String url;
    private String dolby_ch = "";
    private int sliceDelay = 0;
    private int delayS = 0;
    private int delayE = 0;
    private String formatType;
    private Integer status = 0;
    private Timestamp addTime = new Timestamp(System.currentTimeMillis());
    private Timestamp updateTime;

    private int channelLiveType;
    private String streamSpan;
    private String streamType;

    public static final Map<Integer, String> LIVE_STREAM_STATUS_MAP = new HashMap<Integer, String>() {{
        put(0, "未启动");
        put(1, "启动");
        put(2, "停止");
    }};

    public static final Map<String, String> LIVE_STREAM_ISDEFAULT_MAP = new HashMap<String, String>() {{
        put("0", "否");
        put("1", "是");
    }};

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "father_id")
    public Long getFatherId() {
        return fatherId;
    }

    public void setFatherId(Long fatherId) {
        this.fatherId = fatherId;
    }

    @Column(name = "stream_name")
    public String getStreamName() {
        return streamName;
    }

    public void setStreamName(String streamName) {
        this.streamName = streamName;
    }

    @Column(name = "is_default")
    public Boolean getIsDefault() {
        return isDefault;
    }

    public void setIsDefault(Boolean isDefault) {
        this.isDefault = isDefault;
    }

    @Column(name = "uuid")
    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    @Column(name = "url")
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Column(name = "slice_delay")
    public int getSliceDelay() {
        return sliceDelay;
    }

    public void setSliceDelay(int sliceDelay) {
        this.sliceDelay = sliceDelay;
    }

    @Column(name = "delay_s")
    public int getDelayS() {
        return delayS;
    }

    public void setDelayS(int delayS) {
        this.delayS = delayS;
    }

    @Column(name = "delay_e")
    public int getDelayE() {
        return delayE;
    }

    public void setDelayE(int delayE) {
        this.delayE = delayE;
    }

    @Column(name = "format_type")
    public String getFormatType() {
        return formatType;
    }

    public void setFormatType(String formatType) {
        this.formatType = formatType;
    }

    @Column(name = "status")
    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Column(name = "add_time", updatable = false)
    public Timestamp getAddTime() {
        return addTime;
    }

    public void setAddTime(Timestamp addTime) {
        this.addTime = addTime;
    }

    @Column(name = "update_time", insertable = false, updatable = false)
    public Timestamp getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Timestamp updateTime) {
        this.updateTime = updateTime;
    }

    @Transient
    public int getChannelLiveType() {
        return channelLiveType;
    }

    public void setChannelLiveType(int channelLiveType) {
        this.channelLiveType = channelLiveType;
    }

    @Transient
    public String getStreamSpan() {
        return streamSpan;
    }

    public void setStreamSpan(String streamSpan) {
        this.streamSpan = streamSpan;
    }

    @Transient
    public String getStreamType() {
        return streamType;
    }

    public void setStreamType(String streamType) {
        this.streamType = streamType;
    }

    @Column(name = "dolby_ch")
    public String getDolby_ch() {
        return dolby_ch;
    }

    public void setDolby_ch(String dolby_ch) {
        this.dolby_ch = dolby_ch;
    }

    public LiveStream copyNewInstance() {
        LiveStream liveStream = new LiveStream();
        liveStream.setFatherId(this.getFatherId());
        liveStream.setStreamName(this.getStreamName());
        liveStream.setIsDefault(this.getIsDefault());
        liveStream.setUuid(this.getUuid());
        liveStream.setUrl(this.getUrl());
        liveStream.setFormatType(this.getStreamName());
        liveStream.setStatus(this.getStatus());
        liveStream.setSliceDelay(this.getSliceDelay());
        liveStream.setDelayS(this.getDelayS());
        liveStream.setDelayE(this.getDelayE());
        liveStream.setDolby_ch(this.getDolby_ch());
        return liveStream;
    }

    //新奇普
    public String toQipuUrl() {
        String url = "";
        if (FORMATTYPE_FLV.equals(this.formatType)) {
            url = "http://f.live.video.qiyi.com/live/" + this.getUrl() + ".flv?delay=" + this.getSliceDelay() + "&delay_s=" + this.getDelayS() + "&delay_e=" + this.getDelayE();
        } else if (FORMATTYPE_TS.equals(this.formatType)) {
            if(StringUtils.isNotEmpty(this.getDolby_ch())) {
                url = this.getUrl();
            }else {
                url = "http://m3u8.live.video.qiyi.com/tslive/" + this.getUrl() + "/" + this.getUrl() + ".m3u8?pv=0.2";
            }
        } else if (FORMATTYPE_HCDN.equals(this.formatType)) {
            url = this.getUrl() + "&dump_status=1&cdn_set=1&delay=" + this.getSliceDelay() + "&delay_s=" + this.getDelayS() + "&delay_e=" + this.getDelayE();
        } else if (FORMATTYPE_MP4.equals(this.formatType)) {
            url = "http://m3u8.live.video.qiyi.com/tslive/" + this.getUrl() + "/" + this.getUrl() + ".flv";
        } else {
            url = this.getUrl();
        }
        return url;
    }

    //旧奇普
    public String toTvQipuUrl() {
        String url = "";
        if (FORMATTYPE_HCDN.equals(this.formatType)) {
            url = this.getUrl() + "&dump_status=1&cdn_set=1";
        } else if (FORMATTYPE_TS.equals(this.formatType)) {
            url = "http://m3u8.live.video.qiyi.com/tslive/" + this.getUrl() + "/" + this.getUrl() + ".m3u8?pv=0.2";
        } else if (FORMATTYPE_MP4.equals(this.formatType)) {
            url = "http://m3u8.live.video.qiyi.com/tslive/" + this.getUrl() + "/" + this.getUrl() + ".flv";
        } else {
            url = this.getUrl();
        }
        return url;
    }

    public Properties.TvLiveStreamList.TvLiveStreamInfo.FormatType toTvQipuFormatType() {
        if (FORMATTYPE_FLV.equals(this.formatType)) {
            return Properties.TvLiveStreamList.TvLiveStreamInfo.FormatType.FLV;
        } else if (FORMATTYPE_TS.equals(this.formatType)) {
            return Properties.TvLiveStreamList.TvLiveStreamInfo.FormatType.TS;
        } else if (FORMATTYPE_HCDN.equals(this.formatType)) {
            return Properties.TvLiveStreamList.TvLiveStreamInfo.FormatType.HCDN;
        } else if (FORMATTYPE_MP4.equals(this.formatType)){
            return Properties.TvLiveStreamList.TvLiveStreamInfo.FormatType.MP4;
        } else if (FORMATTYPE_P2P.equals(this.formatType)){
            return Properties.TvLiveStreamList.TvLiveStreamInfo.FormatType.P2P;
        } else {
            return null;
        }
    }

    public TLiveStream toThriftEntity() {
        TLiveStream tLiveStream = new TLiveStream();
        tLiveStream.setId(this.getId());
        tLiveStream.setFatherId(this.getFatherId());
        tLiveStream.setStreamName(this.getStreamName());
        if (this.getIsDefault()) {
            tLiveStream.setIsDefault("1");
        } else {
            tLiveStream.setIsDefault("0");
        }
        tLiveStream.setStatus(this.getStatus());
        tLiveStream.setUrl(this.getUrl());
        tLiveStream.setSliceDelay(this.getSliceDelay());
        tLiveStream.setDelayS(this.getDelayS());
        tLiveStream.setDelayE(this.getDelayE());
        tLiveStream.setUuid(this.getUuid());
        tLiveStream.setFormatType(this.getFormatType());
        tLiveStream.setAddTime(this.getAddTime().toString());
        tLiveStream.setUpdateTime(this.getUpdateTime().toString());
        tLiveStream.setDolbyCh(this.getDolby_ch());
        return tLiveStream;
    }
}
